(function() {
    'use strict';

    /** 预约项目类型管理
     * @ngdoc function
     * @name webApp.controller:SchematypeCtrl
     * @description 
     * # SchematypeCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('SchematypeCtrl', SchematypeCtrl);

    SchematypeCtrl.$inject = ['$scope', 'dataService', 'helper', 'dhfprinterService', '$timeout', '$interval', '$rootScope', '$state', 'moment', '$location'];

    function SchematypeCtrl($scope, dataService, helper, dhfprinterService, $timeout, $interval, $rootScope, $state, moment, $location) {
        $scope.listAction = function() {

            $scope.getlist(1);
            $scope.$on('event:up-store-id', function() {
                $scope.store_id = $rootScope.store_id;
                $scope.getlist(1);
            });
        }

        $scope.getlist = function(pageNo) {
            $scope.pageNo = pageNo ? pageNo : "1";
            var query = {
                page_no: $scope.pageNo,
                page_size: $scope.pageSize
            };

            query.query = { store_id: $scope.store_id };

            if ($scope.schema_type_id) {
                query.query.schema_type_id = $scope.schema_type_id;
            }
            
            dataService.schematypeGet(query).then(function(re1) {
                if (re1.schema_types) {
                    $scope.schema_types = re1.schema_types;
                    $scope.schema_types_Total = re1.total_count;
                    if ($scope.schema_type_id) {
                        $scope.schema_type = $scope.schema_types[0];
                    }
                    console.error("schema_types", $scope.schema_types);
                }
            });
        }

        $scope.del = function(id) {
            helper.myConfirm("确定删除?如果删除 此类型下的预约项目都会对应删除！", function() {
                var data = { 'schema_type_id': id };
                dataService.schematypeDelete(data).then(function(result) {
                    if (result.schema_type_id) {
                        // $scope.getList(1);
                        helper.myAlert("success", null, null, function() {
                            $scope.getlist();
                        });
                    } else {
                        helper.myAlert("error:" + result);
                    }
                });
            });
        }


        //添加数据 初始化
        $scope.addAction = function() {
            $scope.schema_type_id = $location.search().schema_type_id;
            if ($scope.schema_type_id) {
                $scope.getlist();
            } else {
                //初始化值
            }

        }

        // schema_type_id
        $scope.saveData = function() {

            var zhuoziApiAry = {
                "add": "schematypeAdd",
                "up": "schematypeUpdate",
            }
            var api = "add";
            if ($scope.schema_type_id) {
                api = "up";
            }

            $scope.schema_type.store_id = $rootScope.store_id;

            dataService[zhuoziApiAry[api]]($scope.schema_type).then(function(r) {
                helper.myAlert("success", null, null, function() {
                    $state.go("schematype.list");
                });
            })
        }
    }
})();